var jp = {
	getElement : function(ele){
		if(typeof(ele) == 'string'){
			return document.getElementById(ele);
		}else{
			return ele;
		}
	},
	init: function () {
		 if(window.XMLHttpRequest)
		 {// code for IE7+, Firefox, Chrome, Opera, Safari
			window.XMLHttpRequest = new XMLHttpRequest();
		 }
		 else
		 {// code for IE6, IE5
			window.XMLHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		 }
	},
	open: function(url, callback) {
		// Obtain XMLHttpRequest object
		var xmlhttp = new XMLHttpRequest();
		if (!xmlhttp || xmlhttp == null) {
			alert('No browser XMLHttpRequest (AJAX) support');
			return;
		}

		// Setup optional async response handling via callback
		var cb = callback;
		var async = false;

		if (cb) {
			// Async mode
			async = true;
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4) {
					if (xmlhttp.status == 200) {
						// Processing statements go here...responseXML
						cb(xmlhttp.responseText);

						// Avoid memory leaks in IE
						// 12.may.2007 thanks to Julio Santa Cruz
						xmlhttp = null;
					} else {
						jp.debug('status',xmlhttp.statusText);
					}
				}
				alert('readyState: '+ xmlhttp.readyState + ' responseText: '+ xmlhttp.responseText + '\n');
			};
		}
		// Open URL
		xmlhttp.open('GET', url, async);

		// Send XML to KW server
		xmlhttp.send(null);
		setTimeout(function() {
				//alert(xmlhttp.status);
			}, 100);
		if (!cb) {
			if (xmlhttp.status != 200) {
				//jp.debug('status',xmlhttp.statusText);
				return null;
			}
			// Sync mode (no callback)
			// alert(xmlhttp.responseText);

			return xmlhttp.responseXML;
		}
	},

	addEvent: function (elm, evType, callback, useCapture) {
		var obj = jp.getElement(elm);
		if (obj.addEventListener) {
			obj.addEventListener(evType, callback, useCapture);
			return true;
		} else if (obj.attachEvent) {
			var r = obj.attachEvent('on' + evType, callback);
			return r;
		} else {
			obj['on' + evType] = callback;
		}
	},
	timestamp: 0,
	messages: new Array(),
	messagesIndex: 0,
	debugOn  : true,
	debugWindow : null,
	debug:function(label,val){
		if(jp.debugOn == false){
			return ;
		}
		// Fetch JS function name if any
		if (jp.debug.caller) {
			funcName = jp.debug.caller.toString()
			funcName = funcName.substring(9, funcName.indexOf(")") + 1)
		}

		var funcName = "none";
		// Create message
		var msg = "-" + funcName + ": " + label + "=" + value

		// Add optional timestamp
		var now = new Date()
		var elapsed = now - jp.timestamp
		if (elapsed < 10000) {
			msg += " (" + elapsed + " msec)"
		}

		jp.timestamp = now;

		// Show.

		if ((jp.debugWindow == null) || jp.debugWindow.closed) {
			jp.debugWindow = window.open("", "p_debugWin", "toolbar=no,scrollbars=yes,resizable=yes,width=600,height=400");
		}
		// Write doc header
		jp.debugWindow.document.writeln('<html><head><title>JPush Debug Window</title></head><body bgcolor=#DDDDDD>');

		// Write the messages
		for (var i = 0; i < jp.messagesIndex; i++) {
			jp.debugWindow.document.writeln('<pre>' + i + ': ' + PL.messages[i] + '</pre>');
		}

		// Write doc footer and close
		jp.debugWindow.document.writeln('</body></html>');
		jp.debugWindow.document.close();
		jp.debugWindow.focus();
	}
};
jp.addEvent(window, 'load', jp.init, false);